package com.yc.system.service.impl;

import com.yc.system.domain.Student;
import com.yc.system.dto.StudentDto;
import com.yc.system.mapper.StudentMapper;
import com.yc.system.mapper.TeacherMapper;
import com.yc.system.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("studentService")
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;


    @Override
    public List<StudentDto> findAll() {
        // 查询所有学生信息
        List<Student> studentList = studentMapper.selectAll();
        for (Student student : studentList) {
            System.out.println(student.toString());
        }

        return studentConvert(studentList);
    }

    @Override
    public boolean updateStudentData(String stuId, Map<String, String> data) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("无数据更新");
        }
        // 获取数据中的教师姓名
        String teaName = data.get("teaName");
        if (teaName != null) {
            // 查询出对应的教师ID
            String teaId = teacherMapper.selectTeacherIdByName(teaName);
            if (teaId == null) {
                throw new IllegalArgumentException("教师姓名无效，未查询到对应的教师ID");
            }
            // 将数据中的教师姓名删除
            data.remove("teaName");

            // 将教师ID加入数据
            data.put("teaId", teaId);
        }

        return studentMapper.updateStudentData(stuId, data);
    }

    @Override
    public boolean updateStudentCollegeLevel(String stuName, String collegeLevel, String stuId) {

        //判断学位及id是否为空
        if (collegeLevel == null || collegeLevel.isEmpty()) {
            return false;
        }
        if (stuId == null || stuId.isEmpty()) {
            return false;
        }

        return studentMapper.updateStudentCollegeLevel(stuId, collegeLevel, stuName);
    }

    @Override
    public boolean deleteStudentInfo(String stuId, String graduationStatus) {
        return studentMapper.deleteStudentInfo(stuId, graduationStatus);
    }

    /**
     * 查询该名教师下的学生信息
     * @param teaId
     * @return
     */
    @Override
    public List<StudentDto> selectAllStudentWithTeacherId(String teaId) {

        List<Student> students = studentMapper.selectAllStudentWithTeacherId(teaId);

        return studentConvert(students);
    }

    @Override
    public Integer selectAllStudentsByTeacherId(String teaId) {
        return studentMapper.selectAllStudentsByTeacherId(teaId);
    }

    /**
     * 学生对象转换
     * @param students
     * @return
     */
    public List<StudentDto> studentConvert(List<Student> students) {
        // 查询教师id和教师姓名
        List<Map<String, String>> mapList = teacherMapper.selectAllTeacherName();
        // 将教师数据构建成 Map 以方便快速查找
        Map<String, String> teacherMap = mapList.stream()
                .collect(Collectors.toMap(map -> map.get("tea_id"), map -> map.get("tea_name")));

        // 将学生信息转换为StudentDto，并直接设置教师姓名
        return students.stream()
                .map(student -> {
                    String teacherName = teacherMap.get(student.getTeaId()); // 获取教师姓名
                    return new StudentDto(
                            student.getStuId(),
                            student.getStuName(),
                            student.getSpecialty(),
                            teacherName,  // 设置教师姓名
                            student.getCollegeName(),
                            student.getCollegeLevel(),
                            student.getGraduationStatus()
                    );
                })
                .collect(Collectors.toList());
    }
}
